package com.aoxin.controller.course.webCourse;


import com.aoxin.model.course.SlfClass;
import com.aoxin.service.inter.course.webCourse.ClassService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/web/classController/")
public class ClassController {

    @Autowired
    ClassService classService;

    /**
     * 新增班级
     * @param slfClass
     * @param res
     * @return
     */
    @LogInfo(content = "班级",operateType = OperateType.INSERT)
    @RequestMapping("insertClass")
    public AjaxRes insertClass(SlfClass slfClass,AjaxRes res){
        try {
            if(!EmptyUtil.objFieldsIsEmpty(slfClass)){
                Integer result = classService.insertClass(slfClass);
                if(result != 0){
                    res.setSucceedMsg("添加成功!");
                }else{
                    res.setFailMsg("添加失败!");
                }
            }else {
                res.setFailMsg("添加教室为空");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 查询班级|条件查询班级
     * @param map
     * @param res
     * @return
     */
    @LogInfo(content = "班级",operateType = OperateType.QUERY)
    @RequestMapping("getClasses")
    public AjaxRes getClasses(@RequestParam Map<String,Object> map, AjaxRes res){
        List<SlfClass> slfClasses = classService.getClasses(map);
        res.setSucceed(slfClasses,Const.DATA_SUCCEED);
        return res;
    }

    /**
     * 通过班级Id删除班级
     * @param classId
     * @param res
     * @return
     */
    @LogInfo(content = "班级",operateType = OperateType.DELETE)
    @RequestMapping("deleteClassById")
    public AjaxRes deleteClassById(Integer classId,AjaxRes res){
        if(classId != null){
            Integer result = classService.deleteClassById(classId);
            if(result != 0){
                res.setSucceedMsg("删除成功!");
            }else{
                res.setFailMsg("删除失败!");
            }
        }else {
            res.setFailMsg("删除数据不能为空");
        }
        return res;
    }

    /**
     * 批量删除班级
     * @param classIds
     * @param res
     * @return
     */
    @LogInfo(content = "班级",operateType = OperateType.DELETE)
    @RequestMapping("deleteClass")
    public AjaxRes deleteClass(@RequestParam("classIds") List<Integer> classIds, AjaxRes res){
        if(classIds.size() > 0&&classIds != null){
            Integer result = classService.deleteClass(classIds);
            if(result != 0){
                res.setSucceedMsg("删除成功!");
            }else{
                res.setFailMsg("删除失败!");
            }
        }else {
            res.setFailMsg("数据不能为空");
        }
        return res;
    }

    /**
     * 修改班级
     * @param slfClass
     * @param res
     * @return
     */
    @LogInfo(content = "班级",operateType = OperateType.UPDATE)
    @RequestMapping("updateClass")
    public AjaxRes updateClass(SlfClass slfClass,AjaxRes res){
        try {
            if(!EmptyUtil.objFieldsIsEmpty(slfClass)){
                Integer result = classService.updateClass(slfClass);
                if(result != 0){
                    res.setSucceedMsg("更新成功!");
                }else {
                    res.setSucceedMsg("更新失败!");
                }
            }else {
                res.setFailMsg("数据不能为空");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return res;
    }


}
